//index.js
import MallService from "../../class/service/MallService";

import Router from "../../class/Router";
import Tips from "../../utils/Tips";


const mallService = new MallService();

const app = getApp()
const constant = require("../../constant.js")
const storage = require("../../utils/storage.js")
const auth = require("../../utils/auth.js")
const util = require("../../utils/util.js")
const notification = require("../../utils/WxNotificationCenter.js");

Page({

  data: {
    redirect: '',
    showMockPage: false
  },

  onLoad: function (options) {
    var that = this;

    // debug or not
    if (options.debug) {
      wx.setEnableDebug({
        enableDebug: true
      })
    }

    // set device info into storage
    try {
      var res = wx.getSystemInfoSync()
      wx.setStorageSync(storage.keys.platform, res.platform)
      wx.setStorageSync(storage.keys.system, res.system)
    } catch (e) {
      console.log(e)
    }

    console.log(options);

    if(options.page) {
      const redirect = constant.redirectUrl[options.page]
      console.log(typeof redirect);

      if (typeof redirect == 'string') {
        that.setData({
          redirect: redirect
        })  
      } else if (typeof redirect == 'function') {
        if (options.page == 'productDetail' || options.page == 'mallDetail') {
          var a = redirect(options.id);
          that.setData({
            redirect: a
          }) 
        }
      }
    }

    console.log(options)

    // do auth here
    that.doAuth();
  },

  onShow: function (options) {
    console.log(options)
  },

  doAuth: function (options) {
    var that = this;

    wx.request({
      url: constant.api(true).url + '/tencent-auth',
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.data.tencetAuth.mock) {
          
          app.globalData.prod = false

          try {
            var unionid = wx.getStorageSync(storage.keys.unionid)
            var mobile = wx.getStorageSync(storage.keys.mobile)
            if (unionid) {
              if (mobile) {
                that.setData({
                  unionid: unionid
                })
                // 授权成功，跳转到电商首页
                wx.redirectTo({
                  url: that.data.redirect ? ('../..' + that.data.redirect) : '../../pages/product/detail/detail'
                })

              } else {
                that.authRequest()
              }
            } else {
              that.authRequest()
            }
          } catch (e) {
            console.log(e)
            console.log("no unionid in storgae start auth")
            that.authRequest()
          }
        } else {
          app.globalData.prod = true
          
          try {
            var unionid = wx.getStorageSync(storage.keys.unionid)
            var mobile = wx.getStorageSync(storage.keys.mobile)
            if (unionid) {
              if (mobile) {
                that.setData({
                  unionid: unionid
                })
                // 授权成功，跳转到电商首页
                wx.redirectTo({
                  url: that.data.redirect ? ('../..' + that.data.redirect) : '../../pages/product/detail/detail'
                })

              } else {
                that.authRequest()
              }
            } else {
              that.authRequest()
            }
          } catch (e) {
            console.log(e)
            console.log("no unionid in storgae start auth")
            that.authRequest()
          }
        }
      },
      fail: function () {
        app.globalData.prod = true
      }
    })
  },

  doUserAuth: function () {
    // save auth reidrect page into storgae
    var that = this;

    wx.showModal({
      title: '登录提示',
      content: '您还未登录注册，登陆后查看已获取的课程信息',
      cancelText: '不需要',
      confirmText: '授权登录',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          Tips.loading();
          try {
            setTimeout(function() {
              Tips.loaded();
            }, 3000)
            auth.getUserInfo({}, function (unionid) {
              that.setData({
                unionid: unionid
              })
              wx.setStorageSync(storage.keys.authRedirect, "")
              setTimeout(function () {
                wx.reLaunch({
                  url: that.data.redirect ? ('../..' + that.data.redirect) : '../../pages/product/detail/detail'
                })
              }, 2000)
            }, function () {
              console.log("auth get userinfo faild");
              wx.redirectTo({
                url: '../../pages/error-page/error-page',
              })
            })
          } catch (e) {
            console.log(e)
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
          Tips.toast("感谢您的使用~")
        }
      }
    })
  },

  authRequest: function () {
    var that = this;
    
    that.setData({
      showMockPage: true
    })

    Tips.loading();

    mallService.list().then(data => {

      Tips.loaded();

      that.setData({
        "skus": data.skus
      })
    }, res => {
      Tips.error("获取商品详情失败");
      that.setData({
        "init": true
      })
    })
  }
})